<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>
	.KEYW {color: #933;}
	.COMM {color: #bbb; font-style: italic;}
	.NUMB {color: #393;}
	.STRN {color: #393;}
	.REGX {color: #339;}
	.line {border-right: 1px dotted #666; color: #666; font-style: normal;}
	</style></head><body><pre><span class='line'>  1</span> <span class="COMM">/**
<span class='line'>  2</span>  * Returns a {@link pv.Flatten} operator for the specified map. This is a
<span class='line'>  3</span>  * convenience factory method, equivalent to &lt;tt>new pv.Flatten(map)&lt;/tt>.
<span class='line'>  4</span>  *
<span class='line'>  5</span>  * @see pv.Flatten
<span class='line'>  6</span>  * @param map a map to flatten.
<span class='line'>  7</span>  * @returns {pv.Flatten} a flatten operator for the specified map.
<span class='line'>  8</span>  */</span><span class="WHIT">
<span class='line'>  9</span> </span><span class="NAME">pv.flatten</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">map</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 10</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">new</span><span class="WHIT"> </span><span class="NAME">pv.Flatten</span><span class="PUNC">(</span><span class="NAME">map</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 11</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 12</span> 
<span class='line'> 13</span> </span><span class="COMM">/**
<span class='line'> 14</span>  * Constructs a flatten operator for the specified map. This constructor should
<span class='line'> 15</span>  * not be invoked directly; use {@link pv.flatten} instead.
<span class='line'> 16</span>  *
<span class='line'> 17</span>  * @class Represents a flatten operator for the specified array. Flattening
<span class='line'> 18</span>  * allows hierarchical maps to be flattened into an array. The levels in the
<span class='line'> 19</span>  * input tree are specified by &lt;i>key&lt;/i> functions.
<span class='line'> 20</span>  *
<span class='line'> 21</span>  * &lt;p>For example, consider the following hierarchical data structure of Barley
<span class='line'> 22</span>  * yields, from various sites in Minnesota during 1931-2:
<span class='line'> 23</span>  *
<span class='line'> 24</span>  * &lt;pre>{ 1931: {
<span class='line'> 25</span>  *     Manchuria: {
<span class='line'> 26</span>  *       "University Farm": 27.00,
<span class='line'> 27</span>  *       "Waseca": 48.87,
<span class='line'> 28</span>  *       "Morris": 27.43,
<span class='line'> 29</span>  *       ... },
<span class='line'> 30</span>  *     Glabron: {
<span class='line'> 31</span>  *       "University Farm": 43.07,
<span class='line'> 32</span>  *       "Waseca": 55.20,
<span class='line'> 33</span>  *       ... } },
<span class='line'> 34</span>  *   1932: {
<span class='line'> 35</span>  *     ... } }&lt;/pre>
<span class='line'> 36</span>  *
<span class='line'> 37</span>  * To facilitate visualization, it may be useful to flatten the tree into a
<span class='line'> 38</span>  * tabular array:
<span class='line'> 39</span>  *
<span class='line'> 40</span>  * &lt;pre>var array = pv.flatten(yields)
<span class='line'> 41</span>  *     .key("year")
<span class='line'> 42</span>  *     .key("variety")
<span class='line'> 43</span>  *     .key("site")
<span class='line'> 44</span>  *     .key("yield")
<span class='line'> 45</span>  *     .array();&lt;/pre>
<span class='line'> 46</span>  *
<span class='line'> 47</span>  * This returns an array of object elements. Each element in the array has
<span class='line'> 48</span>  * attributes corresponding to this flatten operator's keys:
<span class='line'> 49</span>  *
<span class='line'> 50</span>  * &lt;pre>{ site: "University Farm", variety: "Manchuria", year: 1931, yield: 27 },
<span class='line'> 51</span>  * { site: "Waseca", variety: "Manchuria", year: 1931, yield: 48.87 },
<span class='line'> 52</span>  * { site: "Morris", variety: "Manchuria", year: 1931, yield: 27.43 },
<span class='line'> 53</span>  * { site: "University Farm", variety: "Glabron", year: 1931, yield: 43.07 },
<span class='line'> 54</span>  * { site: "Waseca", variety: "Glabron", year: 1931, yield: 55.2 }, ...&lt;/pre>
<span class='line'> 55</span>  *
<span class='line'> 56</span>  * &lt;p>The flatten operator is roughly the inverse of the {@link pv.Nest} and
<span class='line'> 57</span>  * {@link pv.Tree} operators.
<span class='line'> 58</span>  *
<span class='line'> 59</span>  * @param map a map to flatten.
<span class='line'> 60</span>  */</span><span class="WHIT">
<span class='line'> 61</span> </span><span class="NAME">pv.Flatten</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">map</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 62</span> </span><span class="WHIT">  </span><span class="NAME">this.map</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">map</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 63</span> </span><span class="WHIT">  </span><span class="NAME">this.keys</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 64</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 65</span> 
<span class='line'> 66</span> </span><span class="COMM">/**
<span class='line'> 67</span>  * Flattens using the specified key function. Multiple keys may be added to the
<span class='line'> 68</span>  * flatten; the tiers of the underlying tree must correspond to the specified
<span class='line'> 69</span>  * keys, in order. The order of the returned array is undefined; however, you
<span class='line'> 70</span>  * can easily sort it.
<span class='line'> 71</span>  *
<span class='line'> 72</span>  * @param {string} key the key name.
<span class='line'> 73</span>  * @param {function} [f] an optional value map function.
<span class='line'> 74</span>  * @return {pv.Nest} this.
<span class='line'> 75</span>  */</span><span class="WHIT">
<span class='line'> 76</span> </span><span class="NAME">pv.Flatten.prototype.key</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">key</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 77</span> </span><span class="WHIT">  </span><span class="NAME">this.keys.push</span><span class="PUNC">(</span><span class="PUNC">{</span><span class="NAME">name</span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">key</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">value</span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">}</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 78</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">this</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 79</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 80</span> 
<span class='line'> 81</span> </span><span class="COMM">/**
<span class='line'> 82</span>  * Returns the flattened array. Each entry in the array is an object; each
<span class='line'> 83</span>  * object has attributes corresponding to this flatten operator's keys.
<span class='line'> 84</span>  *
<span class='line'> 85</span>  * @returns an array of elements from the flattened map.
<span class='line'> 86</span>  */</span><span class="WHIT">
<span class='line'> 87</span> </span><span class="NAME">pv.Flatten.prototype.array</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 88</span> </span><span class="WHIT">  </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">entries</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">stack</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">keys</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.keys</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 89</span> 
<span class='line'> 90</span> </span><span class="WHIT">  </span><span class="COMM">/* Recursively visits the specified value. */</span><span class="WHIT">
<span class='line'> 91</span> </span><span class="WHIT">  </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">visit</span><span class="PUNC">(</span><span class="NAME">value</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 92</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">keys.length</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 93</span> </span><span class="WHIT">      </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">key</span><span class="WHIT"> </span><span class="KEYW">in</span><span class="WHIT"> </span><span class="NAME">value</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 94</span> </span><span class="WHIT">        </span><span class="NAME">stack.push</span><span class="PUNC">(</span><span class="NAME">key</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 95</span> </span><span class="WHIT">        </span><span class="NAME">visit</span><span class="PUNC">(</span><span class="NAME">value</span><span class="PUNC">[</span><span class="NAME">key</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 96</span> </span><span class="WHIT">        </span><span class="NAME">stack.pop</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 97</span> </span><span class="WHIT">      </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 98</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT"> </span><span class="KEYW">else</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 99</span> </span><span class="WHIT">      </span><span class="NAME">entries.push</span><span class="PUNC">(</span><span class="NAME">stack.concat</span><span class="PUNC">(</span><span class="NAME">value</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>100</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>101</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>102</span> 
<span class='line'>103</span> </span><span class="WHIT">  </span><span class="NAME">visit</span><span class="PUNC">(</span><span class="NAME">this.map</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>104</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">entries.map</span><span class="PUNC">(</span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">stack</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>105</span> </span><span class="WHIT">      </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">m</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>106</span> </span><span class="WHIT">      </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">keys.length</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>107</span> </span><span class="WHIT">        </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">k</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">keys</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">v</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">stack</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>108</span> </span><span class="WHIT">        </span><span class="NAME">m</span><span class="PUNC">[</span><span class="NAME">k.name</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">k.value</span><span class="WHIT"> </span><span class="PUNC">?</span><span class="WHIT"> </span><span class="NAME">k.value.call</span><span class="PUNC">(</span><span class="KEYW">null</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">v</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">v</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>109</span> </span><span class="WHIT">      </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>110</span> </span><span class="WHIT">      </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">m</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>111</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>112</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>113</span> </span></pre></body></html>